package com.wyh.controller.groupfour.admin;

import com.wyh.common.enums.ResultCodeEnum;
import com.wyh.common.exception.CustomException;
import com.wyh.common.result.Result;

import com.wyh.domain.dto.gfive.LoginUser;
import com.wyh.domain.dto.groupfour.chargeIndexDTO.ClaimOnCreditDTO;
import com.wyh.domain.dto.groupfour.chargeIndexDTO.ClaimOnCreditPageDTO;
import com.wyh.domain.dto.groupfour.chargeIndexDTO.CustomerInfoPageDTO;

import com.wyh.domain.po.gfive.User;
import com.wyh.domain.vo.gruopfour.chargeIndexVO.*;
import com.wyh.domain.vo.gruopfour.threemeterVO.PageResult;
import com.wyh.service.appservice.ChargeIndexService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;



@RestController
@Api(tags = "管家端首页")
@Slf4j
public class ChargeIndexController {
    @Autowired
    private ChargeIndexService chargeIndexService;
    private static User getAdminUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null) {
            // 获取用户信息
            LoginUser loginUser = (LoginUser) authentication.getPrincipal();
            return loginUser.getUser();
        } else {
            throw new CustomException(ResultCodeEnum.USER_NOT_LOGIN);
        }
    }

    //管家端首页
    @GetMapping("/chargeIndexAccount")
    @ApiOperation("管家端首页-数量")
    public Result<AccountIndexVO> getAccountIndex() {
        Integer userId = getAdminUser().getId();
        AccountIndexVO accountIndex = chargeIndexService.getAccountIndex(userId);
        return Result.success(accountIndex);
    }


    @GetMapping("/chargeIndexInfo")
    @ApiOperation("管家端首页-管理员组织信息")
    public Result<UserOrganizationNameVO> getUserInfo() {
        UserOrganizationNameVO names = chargeIndexService.getUserWithOrganizationNames();
        return Result.success(names);
    }


    @GetMapping("/chargeIndexDoTask")
    @ApiOperation("管家端首页-管理员代办事务")
    public Result<DoTaskIndexVO> getUserDoTask() {
        Integer userId = getAdminUser().getId();
        DoTaskIndexVO doTaskIndex = chargeIndexService.getDoTaskIndex(userId);
        if (doTaskIndex == null) {
          return Result.error();
        }
        return Result.success(doTaskIndex);
    }


    @PostMapping("/chargeIndexClaimOnCredit")
    @ApiOperation("管家端首页-挂账认领")
    public Result<PageResult<ClaimOnCreditVO>> getClaimOnCredit(@RequestBody ClaimOnCreditPageDTO claimOnCreditPage) {
        PageResult<ClaimOnCreditVO> claimOnCredit = chargeIndexService.getClaimOnCredit(claimOnCreditPage);
        return Result.success(claimOnCredit);
    }

    @PostMapping("/chargeIndexCustomerInfo")
    @ApiOperation("管家端首页-所有客户信息")
    public Result<PageResult<CustomerNameVO>> getCustomerInfo(@RequestBody CustomerInfoPageDTO customerInfoPage) {
        PageResult<CustomerNameVO> customerInfo = chargeIndexService.findCustomersByNameAndTelephone(customerInfoPage);
        return Result.success(customerInfo);
    }

//    @GetMapping("/chargeIndexSingleCustomerInfo")
//    @ApiOperation("管家端首页-单个客户信息")
//    public Result<CustomerNameVO> getSingleCustomerInfo(@RequestParam(value = "id") @ApiParam("客户id") Integer id) {
//        CustomerNameVO singleCustomerInfo = chargeIndexService.getSingleCustomerInfo(id);
//        if (singleCustomerInfo == null) {
//            return Result.error();
//        }
//        return Result.success(singleCustomerInfo);
//    }


    @PostMapping("/chargeIndexClaiming")
    @ApiOperation("管家端首页-点击认领按钮")
    public Result getClaiming(@RequestBody ClaimOnCreditDTO claimOnCreditDTO) {
        int i = chargeIndexService.updatePaymentrecords(claimOnCreditDTO);
        if (i == 0) {
         return Result.error("200","认领失败");
        }
        return Result.success("认领成功");
    }


    @GetMapping("/chargeIndexSingleClaiming")
    @ApiOperation("管家端首页-认领详情")
    public Result<ClaimOnCreditVO> getSingleClaiming(@RequestParam(value = "id") @ApiParam("收款记录id") Integer id) {
        ClaimOnCreditVO singleClaiming = chargeIndexService.getSingleClaiming(id);
        if (singleClaiming == null) {
            return Result.error();
        }
        return Result.success(singleClaiming);
    }
}
